﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dhDataAcess.produceDAL.Stock ;
using dhSystemBLL.produceBLL;
using dhSystemBLL.orderData;
using dhDataAcess.produceLinq.StockLinq ;
using baseSet.importExport;
using baseSet;
using dhDataAcess.baseBLL;
using System.Data.SqlClient;

namespace dhsystem.produceForm.Stock
{
    public partial class FrmorderProduceListAdd : Form
    {
        orderProduceListBLL orderProduceList = new orderProduceListBLL();
        orderProduceListModel orderProduceListRow = new orderProduceListModel();
        orderDetailBLL orderDetail = new orderDetailBLL();
        BindingSource BindingSourceorderProduceListAdd;
        string state;
        string orderDetailNo;
        string unit;
        public FrmorderProduceListAdd(BindingSource _bindingSourceorderProduceList, string _state, string _orderDetailNo,string _unit)
        {
            InitializeComponent();
            BindingSourceorderProduceListAdd = _bindingSourceorderProduceList;
            state = _state;
            orderDetailNo = _orderDetailNo;
            unit = _unit;
        }
        //打开窗口时绑定数据
        private void FrmFrmorderProduceListAdd_Load(object sender, EventArgs e)
        {
            textunit.Text = unit;
            textorderDetailNo .Text = orderDetailNo;
            textproduceNo.Text = "S" + GlobalInfo.Date + "-" + basicSql.getOrderSerail("生产单号");

         
           

           // bindingSourceorderDetail.DataSource = orderDetail.getorderDetailAll();
           // comboBoxorderDetailNo.DataSource = bindingSourceorderDetail;
           // comboBoxorderDetailNo.DisplayMember = "orderDetailNo";
           //comboBoxorderDetailNo .ValueMember  = "orderDetailNo";

            if (state == "编辑")
            {

                textproduceNo.DataBindings.Add("text", BindingSourceorderProduceListAdd, "produceNo");
                textorderDetailNo.DataBindings.Add("text", BindingSourceorderProduceListAdd, "orderDetailNo");
                textqty.DataBindings.Add("text", BindingSourceorderProduceListAdd, "qty");
                textunit.DataBindings.Add("text", BindingSourceorderProduceListAdd, "unit");
                dateTimeproducDate.DataBindings.Add("value", BindingSourceorderProduceListAdd, "producDate");
                textqty.DataBindings.Add("text", BindingSourceorderProduceListAdd, "qty");
                textkg.DataBindings.Add("text", BindingSourceorderProduceListAdd, "kg");
                textM.DataBindings.Add("text", BindingSourceorderProduceListAdd, "m");
                textY.DataBindings.Add("text", BindingSourceorderProduceListAdd, "y");
                textL.DataBindings.Add("text", BindingSourceorderProduceListAdd, "gr");
                textmark.DataBindings.Add("text", BindingSourceorderProduceListAdd, "mark");

            }
        }
        //保存按钮，插入或修改数据
        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (state == "新增")
            {
                try
                {
                    DialogResult result = MessageBox.Show("是否新增生产通知单！", "提示信息", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        orderProduceListRow.produceNo = textproduceNo.Text;
                        orderProduceListRow.orderDetailNo = textorderDetailNo.Text;
                        orderProduceListRow.qty =Convert .ToDecimal ( textqty.Text);
                        orderProduceListRow.unit = textunit.Text;
                        orderProduceListRow.producDate = dateTimeproducDate.Value;
                        orderProduceListRow.orderStauts = "新建";
                        orderProduceListRow.createDate = DateTime.Now;
                        orderProduceListRow.createUser = GlobalInfo.UserName;
                        orderProduceListRow.mark = textmark.Text;
                        orderProduceListRow.gr =Convert .ToDecimal ( textL.Text);
                        orderProduceListRow.kg = Convert.ToDecimal(textkg.Text);
                        orderProduceListRow.m = Convert.ToDecimal(textM.Text);
                        orderProduceListRow.y = Convert.ToDecimal(textY.Text);
                        orderProduceList .orderProduceListInsert (orderProduceListRow );
                        bindingSourceorderProduceList.DataSource = orderProduceList.getorderProduceListAll ();
                        MessageBox.Show("新增成功");
                    }
                }
                catch (FormatException ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
            if (state == "编辑")
            {
                try
                {
                    DialogResult result = MessageBox.Show("是否修改原材料资料！", "提示信息", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        orderProduceListRow.produceNo = textproduceNo.Text;
                        orderProduceListRow.orderDetailNo = textorderDetailNo.Text;
                        orderProduceListRow.qty = Convert.ToDecimal(textqty.Text);
                        orderProduceListRow.unit = textunit.Text;
                        orderProduceListRow.producDate = dateTimeproducDate.Value;
                        orderProduceListRow.orderStauts = "新建";
                        orderProduceListRow.createDate = DateTime.Now;
                        orderProduceListRow.createUser = GlobalInfo.UserName;
                        orderProduceListRow.mark = textmark.Text;
                        orderProduceListRow.gr = Convert.ToDecimal(textL.Text);
                        orderProduceListRow.kg = Convert.ToDecimal(textkg.Text);
                        orderProduceListRow.m = Convert.ToDecimal(textM.Text );
                        orderProduceListRow.y = Convert.ToDecimal(textY.Text);
                        orderProduceList.orderProduceListUpdate (orderProduceListRow);
                        bindingSourceorderProduceList.DataSource = orderProduceList.getorderProduceListAll();
                        MessageBox.Show("修改成功");
                    }
                }
                catch (FormatException ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
        }
        //退出按钮
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();

        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }
        //数量值发生改变时
        private void textqty_TextChanged(object sender, EventArgs e)
        {
            if(unit =="GR")
            {
            textL.Text = textqty .Text ;
            textM.Text = Convert.ToString(Convert.ToDouble(textqty.Text) * Convert.ToDouble(basicSql.getUnitrateM(textunit.Text)));
            textY.Text = Convert.ToString(Convert.ToDouble(textqty.Text) * Convert.ToDouble(basicSql.getUnitrateY(textunit.Text)));
            textkg.Text = Convert.ToString(Convert.ToDouble(textqty.Text) * Convert.ToDouble(basicSql.getUnitrateKg(textunit.Text)));
            }
            if (unit == "M")
            {
                textL.Text = Convert.ToString(Convert.ToDouble(textqty.Text) * Convert.ToDouble(basicSql.getUnitrateL(textunit.Text)));
                textM.Text = textqty.Text;
                textY.Text = Convert.ToString(Convert.ToDouble(textqty.Text) * Convert.ToDouble(basicSql.getUnitrateY(textunit.Text)));
                textkg.Text = Convert.ToString(Convert.ToDouble(textqty.Text) * Convert.ToDouble(basicSql.getUnitrateKg(textunit.Text)));
            }
            if (unit == "Y")
            {
                textL.Text = Convert.ToString(Convert.ToDouble(textqty.Text) * Convert.ToDouble(basicSql.getUnitrateL(textunit.Text)));
                textM.Text = Convert.ToString(Convert.ToDouble(textqty.Text) * Convert.ToDouble(basicSql.getUnitrateM(textunit.Text)));
                textY.Text = textqty.Text;
                textkg.Text = Convert.ToString(Convert.ToDouble(textqty.Text) * Convert.ToDouble(basicSql.getUnitrateKg(textunit.Text)));
            }
        }
        ////选择漂染单号按钮
        //private void buttselectpname_Click(object sender, EventArgs e)
        //{
        //    string field;
        //    field = "orderDetailNo";
        //    Frmprodutselect productselect = new Frmprodutselect(moduleInfo .orderDetail  , field);
        //    DialogResult result = productselect.ShowDialog();
        //    if (result == DialogResult.Yes)
        //    {
        //        textorderDetailNo .Text  = productselect.pname;
        //    }
        //}
    }
}
